var EM = {};
module.exports = EM;

EM.server = require("emailjs/email").server.connect(
    {
        host: process.env.EMAIL_HOST || 'smtp.163.com',
        user: process.env.EMAIL_USER || 'xjtusti@163.com',
        password: process.env.EMAIL_PASS || 'xjtu123456789',
        ssl: true
    });

//Warning: your IDE maybe use .zshrc/.bashrc's old cache, so please restart your IDE.
EM.dispatchResetPasswordLink = function (account, callback) {
    EM.server.send({
        // from: ['XJTU <', process.env.EMAIL_USER, '>'].join('') || 'xjtusti@163.com',
        from: 'xjtusti@163.com',
        to: account['contact-email'],
        subject: '密码重置',
        text: '出了些差错... :(',
        attachment: EM.composeEmail(account)
    }, callback);
};

EM.composeEmail = function (o) {
    var link = 'http://sti.xjtu.edu.cn/reset-password?e=' + o['contact-email'] + '&p=' + o['pass'];
    var html = "<html><body>";
    html += "你好 " + o['inc-name'] + ",<br><br>";
    html += "你的用户名是 <b>" + o.user + "</b><br><br>";
    html += "<a href='" + link + "'>点击这里重置密码</a><br><br>";
    html += "干杯🍻<br>";
    html += "</body></html>";
    return [{data: html, alternative: true}];
};


//Warning: your IDE maybe use .zshrc/.bashrc's old cache, so please restart your IDE.
EM.dispatchUserMsg = function (data, callback) {
    EM.server.send({
        // from: ['XJTU <', process.env.EMAIL_USER, '>'].join('') || 'Node Login <do-not-reply@gmail.com>',
        from: 'xjtusti@163.com',
        to: data['contact-email'],
        subject: data['subject'],
        text: '',
        attachment: EM.composeMsgEmail(data)
    }, callback);
};

EM.composeMsgEmail = function (data) {
    var html = "<html><body>";
    html += "你好 " + data['inc-name'] + ",<br><br>";
    html += "你的用户名是 <b>" + data['user'] + "</b><br><br>";
    html += data['msg'] + "🍻<br>";
    html += "</body></html>";
    return [{data: html, alternative: true}];
};